// pages/contentDetail/contentDetail.js
import {
  addContentMsg,
  getActivityContentMessage,
} from "../../services/activityDetailReq";

const { addMsg } = require("../../services/activityDetailReq");
const { getMessage } = require("../../services/activityDetailReq");
const { getDetail } = require("../../services/contentDetailReq");
const { getActivity } = require("../../services/activityDetailReq");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    obj: {},
    activityObj: null,

    pageNum: 1,
    pageSize: 10,
    jobId: 0,
    jobShow: false,
    jobList: [],
    showComment: false,
    commentVaule: "",
    userId: null,
    familyInfo: [],
    MemberId: null,
    show: false,
    targetActivityId: null,
    message: [],
    activityContentId: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.Loading("加载中~");
    const id = options.id;
    Promise.all([
      this.handleGetContent(id),
      this.handleGetActivity(),
      this.handleGetContentMessage(id),
    ]);
  },
  //  获取风采详情
  handleGetContent(id) {
    return getDetail(id, (res) => {
      if (res.data.code !== 0) {
        wx.showToast({
          title: res.data.msg,
          icon: "none",
        });
      } else {
        res.data.data.activitycontentDisc = `
        <div class="container" style="padding:12px;">    
        ${res.data.data.activitycontentDisc.replace(/figure/g,"div")}
        </div>
        `;
        
        this.setData({
          obj: res.data.data,
          activityContentId: id,
        });
        wx.hideLoading();
      }
    });
  },
  // 获取留言
  handleGetContentMessage(id) {
    return getActivityContentMessage(
      id,
      this.data.pageNum,
      this.data.pageSize,
      (res) => {
        this.setData({
          message: res.data.data.list,
        });
      }
    );
  },
  // 获取活动信息
  handleGetActivity() {
    // 获取活动详情
    const { activityId } = this.data.obj;
    if (!activityId) return;
    return this.handleGetActivityInfo(activityId);
  },

  handleGetActivityInfo(activityId) {
    return getActivity(activityId, (res) => {
      if (res.data.code !== 0) {
        return wx.showToast({
          title: "出错啦",
          icon: "error",
        });
      }
      let list = res.data;
      try {
        list.activity.activityAddress = JSON.parse(
          list.activity.activityAddress
        );
      } catch (e) {
        console.error(" 数据Bug ");
      }
      this.setData({
        activityObj: list,
      });
    });
  },

  // 写留言
  handleWriteMessage() {
    const volunteerId = wx.getStorageSync("volunteerInfo").volunteerId;
    if (!volunteerId)
      return wx.showToast({
        title: "请先登陆",
        icon: "error",
        duration: 2000,
        success(res) {
          setTimeout(
            () =>
              wx.navigateTo({
                url: "../login/login",
              }),
            2000
          );
        },
      });
    this.setData({
      showComment: true,
    });
  },

  handleComment(e) {
    wx.showLoading({
      title: "留言中",
      icon: "loading",
    });
    const activitycontentId = this.data.activityContentId;
    const messageContent = this.data.commentVaule;
    const volunteerId = wx.getStorageSync("volunteerInfo").volunteerId;
    if (!volunteerId) {
      return wx.showToast({
        title: "跳转登陆",
        duration: 1000,
        success(res) {
          setTimeout(
            () =>
              wx.navigateTo({
                url: "/pages/login/login",
              }),
            1000
          );
        },
      });
    }

    let obj = {
      activitycontentId: activitycontentId,
      commentContent: messageContent,
      volunteerId,
    };
    // console.log(`obj-->`, obj)
    if (!messageContent) {
      return wx.showToast({
        title: "请输入内容",
        icon: "error",
      });
    }
    addContentMsg(obj, (res) => {
      if (res.data.code !== 0) {
        wx.showToast({
          title: res.data.msg,
          icon: "none",
        });
      } else {
        wx.showToast({
          title: "留言成功",
        });
        getActivityContentMessage(
          obj.activitycontentId,
          this.data.pageNum,
          this.data.pageSize,
          (res) => {
            this.setData({
              message: res.data.data.list,
            });
          }
        );
        this.setData({
          showComment: false,
          commentVaule: "",
        });
      }
    });
  },

  handleCommentChange(e) {
    this.setData({
      commentVaule: e.detail.value,
    });
  },
  handleCommentClose() {
    this.setData({
      commentVaule: "",
      showComment: false,
    });
  },
});
